package com.moran.controller;

import io.modelcontextprotocol.client.McpAsyncClient;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.spec.McpSchema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class WeatherController {

    @Autowired
    private SyncMcpToolCallbackProvider toolCallbackProvider;

    private final List<McpSyncClient> mcpSyncClients;

    private final List<McpAsyncClient> mcpAsyncClients;

    @Autowired
    public WeatherController(List<McpSyncClient> mcpSyncClients, List<McpAsyncClient> mcpAsyncClients) {
        this.mcpSyncClients = mcpSyncClients;
        this.mcpAsyncClients = mcpAsyncClients;
    }

    @GetMapping("/getWeather")
    public String getWeather() {
        McpSyncClient client = mcpSyncClients.get(0);
        McpSchema.ListToolsResult toolsList = client.listTools();
        log.info("Available Tools = " + toolsList);
        log.info("-----------------------------------------------------------------------------------------");

        toolsList.tools().stream().forEach(tool -> {
            log.info("Tool: " + tool.name() + ", description: " + tool.description() + ", schema: " + tool.inputSchema());
        });
        log.info("-----------------------------------------------------------------------------------------");

        McpSchema.CallToolResult weatherForcastResult = client.callTool(new McpSchema.CallToolRequest("getWeatherForecastByLocation",
                Map.of("latitude", "47.6062", "longitude", "-122.3321")));
        log.info("Weather Forcast: " + weatherForcastResult);
        log.info("-----------------------------------------------------------------------------------------");

        McpSchema.CallToolResult alertResult = client.callTool(new McpSchema.CallToolRequest("getAlerts", Map.of("state", "NY")));
        log.info("Alert Response = " + alertResult);
        log.info("-----------------------------------------------------------------------------------------");
        return "";
    }

}
